using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace SMS.Common.IO
{
    public static class Compression
    {
        public static void CompressTextFile(string SourceFileName, string DestinationFileName)
        {
            FileStream sourceFile = new FileStream(SourceFileName, FileMode.Open);
            FileStream destinationFile = new FileStream(DestinationFileName, FileMode.OpenOrCreate);
            GZipStream compressedStream = new GZipStream(destinationFile, CompressionMode.Compress);

            byte[] buffer = new byte[sourceFile.Length];
            sourceFile.Read(buffer, 0, buffer.Length);
            sourceFile.Close();

            compressedStream.Write(buffer, 0, buffer.Length);
            compressedStream.Close();
            destinationFile.Close();

            //int theByte;
            //while ((theByte = sourceFile.ReadByte()) != -1)
            //{
            //    compressedStream.WriteByte((byte)theByte);
            //}

            //compressedStream.Flush();
            //compressedStream.Close();
            //sourceFile.Close();
            //destinationFile.Close();

        }

        public static void DeCompressTextFile(string SourceFileName, string DestinationFileName)
        {
            FileStream sourceFile = new FileStream(SourceFileName, FileMode.Open);
            FileStream destinationFile = new FileStream(DestinationFileName, FileMode.OpenOrCreate);
            GZipStream decompressedStream = new GZipStream(sourceFile, CompressionMode.Decompress);

            byte[] buffer = new byte[4096];
            int size = 0;
            while (true)
            {
                size = decompressedStream.Read(buffer, 0, buffer.Length);
                if (size > 0)
                {
                    destinationFile.Write(buffer, 0, buffer.Length);
                }
                else
                {
                    break;
                }
            }

            //int theByte;
            //while ((theByte = decompressedStream.ReadByte()) != -1)
            //{
            //    destinationFile.WriteByte((byte)theByte);
            //}

            destinationFile.Flush();
            decompressedStream.Close();
            sourceFile.Close();
            destinationFile.Close();
        }
    }
}
